<%@page import="content.CategoryContent"%>
<%@page import="transfer.CommodityCategory"%>
<%@page import="content.CommodityContent"%>
<%@page import="transfer.Commodity"%>
<%@page import="java.util.ArrayList"%>

<%
    int category = 1;
    int pageCount = 1;
    int count = 12;
    
    try {
        String categoryParameter = request.getParameter("type");
        String pageParameter = request.getParameter("page");
        if (categoryParameter != null) {
            category = Integer.parseInt(categoryParameter);
        }
        if (pageParameter != null) {
            pageCount = Integer.parseInt(pageParameter);
        }
    } catch (NumberFormatException nfe) {
        nfe.printStackTrace();
    } catch (NullPointerException npe) {
        npe.printStackTrace();
    }
    
    CommodityCategory commodityCategory = CategoryContent.getCategory(new Integer(category));
    if (commodityCategory == null) {
        category = 1;
        commodityCategory = CategoryContent.getCategory(new Integer(category));
    }
    int goodsAmount = CommodityContent.getAmountGoodsOfCategory(new Integer(category));
    int pages = (int) (goodsAmount / count);
    if (goodsAmount % count != 0) {
        pages++;
    }
    if (pageCount > pages)
        pageCount = pages;
%>

<div class="block">
    <div class="block-header"><%= commodityCategory.getNameCategory()%></div>

    <%
        ArrayList goods = CommodityContent.getGoodsByCategory(category, pageCount, count);
        double price = 0;
        for (int i = 0; i < goods.size(); i++) {
            Commodity c = (Commodity) goods.get(i);
            String imagePath = "images/commodity/" + c.getImage();
    %>
    <div class="box-prod" id="<% out.print(c.getIdCommodity());%>">    
        <a href="index.jsp?content=view&id=<%= c.getIdCommodity()%>">
            <div class="img-cont"><img src="<% out.print(imagePath);%>"></div>

            <h4><% out.print(c.getNameCommodity());%></h4>
        </a>
        <p>
            <% price = c.getPrice().doubleValue();
                if ((Math.round(price) - price) == 0) {
                    out.print((int) price + " $");
                } else {
                    out.print(price + " $");
                }
            %> 
        </p>
        <%
            if (!c.getOutOfProduction().booleanValue()) {
        %>
        <div class="btn-buy-cont" id="buy_button" onclick="addCommodity($(this).parent().attr('id'));" ><input type="button" value="buy" class="btn-buy" /></div>
            <%            }
            %>
    </div>    

    <%
        }
    %>
</div>

<%
//int goodsAmount = CommodityContent.getAmountGoodsOfCategory(new Integer(category));
//int pages = (int) (goodsAmount/count);
//if (goodsAmount%count != 0)
//    pages++;
%>

<div id="pagination">
    <%
        if (pageCount > 1) {
    %>

    <a href="index.jsp?content=category&type=<%= category%>&page=1"><<</a>

    <%
    } else {
    %>      
    <span id="selected-page"><<</span>
    <%        }
        if (pageCount > 1) {
    %>

    <a href="index.jsp?content=category&type=<%= category%>&page=<%= pageCount - 1%>"><</a>

    <%
    } else {
    %>      

    <span id="selected-page"><</span>

    <%        }

        if (pageCount - 1 > 0) {
    %>

    <a href="index.jsp?content=category&type=<%= category%>&page=<%= pageCount - 1%>"><%= pageCount - 1%></a>

    <%
        }
    %>

    <span id="selected-page"><%= pageCount%></span>

    <%
        if (pageCount + 1 <= pages) {
    %>

    <a href="index.jsp?content=category&type=<%= category%>&page=<%= pageCount + 1%>"><%= pageCount + 1%></a>

    <%
        }
        if (pages != pageCount) {
    %>

    <a href="index.jsp?content=category&type=<%= category%>&page=<%= pageCount + 1%>">></a>

    <%
    } else {
    %>
    <span id="selected-page">></span>
    <%        }
    %>


    <%
        if (pageCount != pages) {
    %>
    <a href="index.jsp?content=category&type=<%= category%>&page=<%= pages%>">>></a>
    <%
    } else {
    %>      
    <span id="selected-page">>></span>
    <%        }
    %>

    <div id="results">Show <%= pageCount%> of <%= pages%> pages</div>
</div>